package com.imooc.demo.web;

import com.imooc.demo.entity.Talk;
import com.imooc.demo.service.TalkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/talk")
public class TalkController {
    @Autowired
    TalkService talkService;

    ///////创建留言
    @PostMapping("/insertTalk")
    public Map<String, Object> insertTalk(@RequestParam("buyerOpenid") String buyerOpenid,
                                          @RequestParam("talkText") String talkText,
                                          @RequestParam("picture") String picture,
                                          @RequestParam("title") String title) {

        System.out.println("进来了");
        Talk talk = new Talk();
        talk.setBuyerOpenid(buyerOpenid);
        talk.setTalkText(talkText);
        talk.setPicture(picture);
        talk.setTitle(title);
        System.out.println("talk:"+talk.toString());
        // 添加到数据库
        Boolean flag = talkService.insertTalk(talk);
        if(!flag){
            System.out.println("插入数据库talk失败！");
        }else {
            System.out.println("插入数据库talk成功！");
        }
        // 封装返回小程序
        Map<String, Object> result = new HashMap<>();
        result.put("success", flag);
        return  result;
    }

    //通过buyerOpenid获取留言信息
    @RequestMapping(value = "talkList",method = RequestMethod.GET)
    private  Map<String,Object>  talkList( @RequestParam("buyerOpenid") String buyerOpenid
    ){
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        List<Talk> talkList= talkService.talkList(buyerOpenid);

        System.out.println("talkList:"+talkList.toString());
        modelMap.put("talkList",talkList);
        return modelMap;
    }


    //用户删除
    @RequestMapping(value = "/deleteTalk",method = RequestMethod.GET)
    //@RequestBody标识接收的内容除了xwww from url encoding 编码内容之外的一些内容，如json，xml等,此处需传入xml
    private  Map<String,Object>  deleteTalk(@RequestParam("talkId") Integer talkId){
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        System.out.println("talkId:"+talkId);
        Talk talk = talkService.getTalk(talkId);
        talk.setStatus(1);
        System.out.println(talk);

        modelMap.put("success",talkService.deleteTalk(talk));
        return modelMap;
    }

    //所有Talk
    @RequestMapping(value = "allTalkList",method = RequestMethod.GET)
    private  Map<String,Object>  allTalkList(){
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        List<Talk> talkList= talkService.allTalkList();

        System.out.println("talkList:"+talkList.toString());
        modelMap.put("talkList",talkList);
        return modelMap;
    }

    //所有Talk
    @RequestMapping(value = "getTalk",method = RequestMethod.GET)
    private  Map<String,Object>  getTalk(@RequestParam("talkId") Integer talkId){
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        Talk talk= talkService.getTalk(talkId);

        System.out.println("talk:"+talk.toString());
        modelMap.put("talk",talk);
        return modelMap;
    }


}
